<template>
	<view class="documentListPage">
		<view class="searchContent">
			<u-input v-model="keyWord" shape="circle" inputAlign="center" placeholder="搜索" @confirm="doSearch">
				<view slot="suffix" class="iconClass" @click="doSearch">
					<u-icon name="search" color="#004DA1" size="52rpx"></u-icon>
				</view>
			</u-input>
		</view>
		<view class="tabAndFilter" v-if="isLoad">
			<view class="tabContent">
				<view @click="doFilter(item)" v-for="(item,index) in tabs" :key="index" class="tabItem"
					:class="activeTab===item.id?'isActive':''">{{item.classificationName}}</view>
			</view>
		</view>
		<view class="documentContent" v-if="isLoad">
			<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" v-if="!list3.length && !isLoading"
				text="没有更多了"></u-empty>
			<view class="documentList" v-for="(item,index) in list3" :key="index">
				<view class="status" v-if="item.isExchange">
					<u--image src="/static/vOnTrial/1.png" shape="square" mode="aspectFill" width="82rpx"
						height="82rpx">
					</u--image>
				</view>
				<view class="image">
					<u--image :src="getImgUrl(item.uploadUrl).imageUrl" shape="square" mode="aspectFill" width="116rpx"
						height="116rpx">
					</u--image>
				</view>
				<view class="documentInfo">
					<view class="title">{{item.curriculumName}}</view>
					<view class="info">
						<view class="author">{{item.curriculumAuthor}}</view>
						<view class="createtime">{{item.createTime}}</view>
					</view>
					<view class="cost">兑换：{{item.integral||0}}积分</view>
				</view>
				<view class="documentBtn" :class="item.isExchange?'color0':'color2'" @click="doChange(item)">{{item.isExchange?'发送邮箱':'积分兑换'}}</view>
			</view>
			<u-loadmore v-if="list3.length" :status="isFinish?'nomore':'loading'" loading-text="正在加载"
				loadmore-text="加载更多" nomore-text="没有更多了" color="#d0d0d0" />
		</view>
		<u-popup :show="showExchange" :round="10" mode="bottom" @close="doClose">
			<view class="exchangePanel">
				<view class="title">积分兑换</view>
				<view class="cost">{{exchangeInfo.integral}}
					<view class="unit">积分</view>
				</view>
				<view class="detail">
					<view class="userScore" v-if="exchangeInfo.canExchange">当前积分{{userScore}}</view>
					<view class="userScore" v-if="!exchangeInfo.canExchange">当前积分不足，请到任务中心获取积分</view>
					<view class="vBtn" @click="doExchange(exchangeInfo)">{{exchangeInfo.btnText}}</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showExchange: false,
				userScore: 0,
				exchangeInfo: {
					cost: "0",
					canExchange: false,
					btnText: "做任务"
				},
				keyWord: "",
				activeTab: "",
				tabs: [],
				list3: [],
				pageNo: 1,
				pageSize: 10,
				total: 0,
				isFinish: false,
				isLoading: false,
				isLoad: false,
				userId: "",
				baseUrl: this.$api.baseUrl,
				colorIndex: 0
			}
		},
		onReachBottom() {
			if (!this.isLoading && !this.isFinish) {
				this.pageNo++;
				this.initData();
			}
		},
		onPullDownRefresh() {
			this.doSearch();
		},
		onLoad() {
			uni.getStorage({
				key: "userInfo",
				success: (userInfo) => {
					this.userId = userInfo.data.id;
					this.$request.get(this.$api.getUserById, {
						id: this.userId
					}).then(result => {
						this.userScore = result.result.integral;
						this.myEmail = result.result.educationMailbox;
						uni.hideLoading()
					})
					this.$request.get(this.$api.studyColumnList, {
						pageNo: 1,
						pageSize: 100
					}).then(res => {
						this.tabs = res.result.records;
						this.tabs.unshift({
							classificationName: "全部",
							id: ""
						})
						//this.activeTab = this.tabs[0].id;
						this.initData();
						uni.hideLoading()
					})
				}
			})
		},
		onShow() {
			this.refreshUser()
		},
		methods: {
			initData(isRefresh) {
				this.isLoading = true;
				Promise.all([
					this.$request.get(this.$api.studyList, {
						type: '2',
						pageNo: this.pageNo,
						pageSize: this.pageSize,
						consultingId: this.activeTab,
						curriculumName: this.keyWord,
						userId: this.userId
					}),
				]).then(res => {
					if (isRefresh) {
						uni.stopPullDownRefresh()
					}
					this.isLoading = false;
					this.list3 = this.list3.concat(res[0].result.records)
					this.total = res[0].result.total;
					if (this.pageNo * this.pageSize < this.total) {
						this.isFinish = false
					} else {
						this.isFinish = true
					}
					this.isLoad = true;
					uni.hideLoading()
				})
			},
			refreshUser() {
				if (this.isLoad) {
					console.log("刷新邮箱和积分数据")
					this.$request.get(this.$api.getUserById, {
						id: this.userId
					}).then(result => {
						uni.setStorage({
							key: "userInfo",
							data: result.result
						})
						this.userScore = result.result.integral;
						this.myEmail = result.result.educationMailbox;
						uni.hideLoading()
					})
				}
			},
			getDocument() {
				this.$request.get(this.$api.studyList, {
					type: '2',
					pageNo: 1,
					pageSize: this.pageSize,
					consultingId: this.activeTab,
					curriculumName: this.keyWord,
					userId: this.userId
				}).then(res => {
					this.list3 = res.result.records;
					uni.showToast({
						icon: "success",
						title: "兑换成功"
					})
					this.showExchange = false;
				})
			},
			getImgUrl(item) {
				let that = this
				let arr = item.split(".");
				let type = arr[arr.length - 1];
				let obj = {};
				switch (type) {
					case "zip":
						obj.imageUrl = '/static/growup/2.png'
						// obj.colorIndex = 0
						break;
					case "doc":
						obj.imageUrl = '/static/growup/3.png'
						// obj.colorIndex = 2
						break;
					case "docx":
						obj.imageUrl = '/static/growup/3.png'
						// obj.colorIndex = 2
						break;
					case "pdf":
						obj.imageUrl = '/static/growup/4.png'
						// obj.colorIndex = 1
						break;
					case "xls":
						obj.imageUrl = '/static/growup/5.png'
						// obj.colorIndex = 0
						break;
					case "xlsx":
						obj.imageUrl = '/static/growup/5.png'
						// obj.colorIndex = 0
						break;
					case "ppt":
						obj.imageUrl = '/static/growup/6.png'
						// obj.colorIndex = 0
						break;
					default:
						obj.imageUrl = '/static/growup/8.png'
						// obj.colorIndex = 0
						break;
				}
				return obj
			},
			doSearch() {
				this.isFinish = false;
				if (!this.isLoading) {
					this.pageNo = 1;
					this.list3 = [];
					this.initData(true);
				}
			},
			doFilter(item) {
				if (this.activeTab === item.id) return;
				this.activeTab = item.id;
				this.doSearch()
			},
			doClose() {
				this.showExchange = false
			},
			doChange(item) {
				if (item.isExchange) {
					if (this.myEmail) {
						this.$request.get(this.$api.sendEmailFile, {
							email: this.myEmail,
							title: item.curriculumName,
							content: "点击附件下载文件",
							filepath: this.baseUrl + '/sys/common/static/' + item.uploadUrl,
							filename: item.uploadUrl
						}).then(res => {
							uni.showToast({
								icon: "success",
								title: "发送成功"
							})
							uni.hideLoading()
						})
					} else {
						uni.showModal({
							title: '温馨提示',
							content: '您还没有绑定邮箱，请去个人中心绑定邮箱账号',
							confirmText: "前去绑定",
							cancelText: "放弃领取",
							success: (res) => {
								if (res.confirm) {
									uni.navigateTo({
										url: "/pages/myInfo/myInfo"
									})
								} else if (res.cancel) {
									uni.showToast({
										icon: "none",
										title: "已取消发送"
									})
								}
								uni.hideLoading()
							}
						})
					}

				} else {
					this.showExchange = true;
					if (item.integral > this.userScore) {
						this.exchangeInfo.id = item.id;
						this.exchangeInfo.canExchange = false;
						this.exchangeInfo.btnText = "做任务"
						this.exchangeInfo.integral = item.integral
					} else {
						this.exchangeInfo.id = item.id;
						this.exchangeInfo.canExchange = true;
						this.exchangeInfo.btnText = "立即兑换"
						this.exchangeInfo.integral = item.integral
					}
				}
			},
			doExchange(item) {
				if (item.integral > this.userScore) {
					uni.navigateTo({
						url: "/pages/taskCenter/taskCenter"
					})
				} else {
					this.$request.post(this.$api.doExchange, {
						integral: item.integral,
						otherId: item.id,
						userId: this.userId,
						exchangeType: 2
					}).then(res => {
						this.getDocument();
						this.refreshUser();
						uni.hideLoading()
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	.documentListPage {
		.searchContent {
			padding: 0rpx 40rpx 26rpx;
			background-color: #FAFAFA;

			.iconClass {
				border-left: 1rpx solid #D0D0D0;
				padding: 0 0 0 38rpx;
			}
		}

		.tabAndFilter {
			display: flex;
			justify-content: flex-start;

			.tabContent {
				display: flex;
				flex-wrap: nowrap;
				justify-content: flex-start;
				padding: 0 40rpx 20rpx;
				width: calc(100vw - 120rpx);
				overflow: auto;

				.tabItem {
					background: #F4F4F4;
					border-radius: 4px 4px 4px 4px;
					color: #333333FF;
					font-size: 26rpx;
					height: 62rpx;
					padding: 0 20rpx;
					line-height: 62rpx;
					margin-right: 20rpx;
					flex-shrink: 0;
				}

				.isActive {
					color: #004DA1FF;
					background-color: #E5EDFFFF;
				}
			}

		}

		.documentContent {
			padding: 10rpx 40rpx 40rpx;
			display: flex;
			justify-content: flex-start;
			flex-direction: column;

			.documentList {
				background: #FFFFFF;
				box-shadow: inset 0px 0px 0px 1rpx rgba(230, 230, 230, .5);
				border-radius: 20rpx;
				margin-bottom: 20rpx;
				display: flex;
				flex-wrap: nowrap;
				justify-content: flex-start;
				align-items: center;
				padding: 30rpx 26rpx;
				position: relative;

				.status {
					position: absolute;
					top: 0;
					left: 0;
				}

				.image {
					margin-right: 26rpx;
				}

				.documentInfo {
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					height: 126rpx;

					.title {
						font-family: PingFang SC-Bold, PingFang SC;
						font-weight: bold;
						color: #333333;
						font-size: 26rpx;
					}

					.info {
						font-size: 26rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #666666;
						display: flex;
						flex-wrap: nowrap;

						.author {
							position: relative;
							padding-right: 8rpx;

							&:after {
								content: "";
								display: inline-block;
								height: 22rpx;
								width: 2rpx;
								background-color: #BDBDBDFF;
								position: absolute;
								right: 0;
								top: calc(50% - 11rpx);
							}
						}

						.createtime {
							padding-left: 8rpx;
						}
					}

					.cost {
						font-size: 26rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #EC6C2F;
					}
				}

				.documentBtn {
					position: absolute;
					right: 20rpx;
					background: linear-gradient(180deg, #87C5FC 0%, #247FFF 100%);
					border-radius: 34rpx;
					height: 50rpx;
					width: 136rpx;
					line-height: 50rpx;
					font-size: 24rpx;
					color: #fff;
					text-align: center;
				}
				.color0 {
					background: linear-gradient(180deg, #87C5FC 0%, #247FFF 100%);
				}
				.color1 {
					background: linear-gradient(180deg, #F3D748 0%, #EF7B17 100%);
				}
				.color2 {
					background: linear-gradient(180deg, #F79037 0%, #F24929 100%);
				}
			}
		}

		.exchangePanel {
			display: flex;
			text-align: center;
			flex-direction: column;

			.title {
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #666666;
				font-size: 28rpx;
				border-bottom: 1rpx solid #eee;
				height: 100rpx;
				line-height: 100rpx;
			}

			.cost {
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				font-size: 46rpx;
				color: #EC6C2FFF;
				display: flex;
				flex-wrap: nowrap;
				justify-content: center;
				border-bottom: 1rpx solid #eee;
				height: 100rpx;
				line-height: 100rpx;

				.unit {
					font-size: 24rpx;
					position: relative;
					top: 6rpx;
					left: 2rpx;
					color: #333333FF;
				}
			}

			.detail {
				line-height: 100rpx;
				display: flex;
				align-items: center;
				flex-direction: column;
				padding-bottom: 40rpx;

				.userScore {
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #999999;
					font-size: 30rpx;
				}

				.vBtn {
					background: linear-gradient(180deg, #87C5FC 0%, #247FFF 100%);
					border-radius: 48rpx;
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: bold;
					color: #FFFFFF;
					font-size: 34rpx;
					height: 96rpx;
					width: 680rpx;
				}
			}
		}
	}
</style>
